//app.js
App({


  /**
   * 获取地址
   */
  getLocation: function() {
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res.latitude);
        wx.request({
          url: 'https://www.xiaozheng8.com/receiveUser/update',
          method: 'post',
          data: {
            userId: wx.getStorageSync("userId"),
            lat: res.latitude,
            lng: res.longitude,
          },
          success: function(res) {},

        })

      }
    })
  },

  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function() {
    // if(wx.getStorage({
    //   key: 'info',
    //   success: function(res) {
    //     console.log(res);
    //     if(res==""||res==undefined){
    //       wx.reLaunch({
    //         url: '../changeId/changeId',
    //       })
    //     }
    //   },
    // }))
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res);
        wx.request({
          url: 'https://www.xiaozheng8.com/receiveUser/update',
          method: 'post',
          data: {
            userId: wx.getStorageSync("userId"),
            lat: res.latitude,
            lng: res.longitude,
            // addrName:res.name,
            // addrDes:res.address
          },
          success: function(res) {
            console.log(res);
          },
          fail(res) {
            console.log(res);

          }
        })

      },
      fail(res) {

      }
    })


  },

  getUserId: function() {
    // var that = this;
    // wx.login({
    //   success: function (res) {
    //     var code = res.code;
    //     console.log(code);
    //     wx.request({
    //       // url: 'https://www.xiaozheng8.com/user/register',
    //       url: 'https://www.xiaozheng8.com/user/login',

    //       method: "post",
    //       data: {
    //         code: res.code,

    //       },
    //       success: function (res) {
    //         console.log(res);
    //         wx.setStorageSync("userId", res.data.data.userId)
    // that.setData({
    //   id: wx.getStorageSync("userId")
    // });
    // if (that.data.id == "" || that.data.id == null | that.data.id == undefined || that.data.id == 0) {
    //   wx.showToast({
    //     title: '加载中',
    //     icon: 'loading',
    //     mask: true
    //   })
    // }
    //       },

    //     })
    //   }

    // })
  },
  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */

  onShow: function(options) {

    // 针对iphoneX的兼容
    let that = this;
    wx.getSystemInfo({
      success: res => {
        // console.log('手机信息res'+res.model)
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          that.globalData.isIphoneX = true
        }

      }
    })
    // console.log(that.globalData.myUserInfo);
    wx.login({
      success: function(res) {
        console.log(res);
        var code = res.code;
        console.log(code);
        that.globalData.code = code;
        wx.getStorage({
          key: 'info',
          success: function(res) {
            console.log(res);
         
            that.globalData.myUserInfo = true;
           
            wx.request({
              url: 'https://www.xiaozheng8.com/user/login',
              data: {
                code: that.globalData.code,
                iv: res.data.iv,
                encryptedData: res.data.encryptedData
              },
              method: "post",
              success(req) {
                  console.log("+++")
                console.log(req)
                wx.setStorageSync("userId", req.data.data.userId)
              },
              fail(req) {
                console.log(req)
              }
            })
            return false
          },
          fail(err) {
            console.log(err);
          }
        })
        if (that.globalData.myUserInfo == false) {
          wx.setStorageSync("userId", "")
        }

        // 登录

        //     wx.request({
        //       // url: 'https://www.xiaozheng8.com/user/register',
        //       url: 'https://www.xiaozheng8.com/user/login',

        //       method: "post",
        //       data: {
        //         code: res.code,

        //       },
        //       success: function (res) {
        //         console.log(res);
        //         wx.setStorageSync("userId", res.data.data.userId);
        //         that.globalData.phone=res.data.data.userPhone

        //       }
        //     })
      }
    })

  },
  openConfirm: function() {
    wx.showModal({
      content: '检测到您没打开的定位权限，是否去设置打开？',
      confirmText: "确认",
      cancelText: "取消",
      success: function(res) {
        console.log(res);
        //点击“确认”时打开设置页面
        if (res.confirm) {
          console.log('用户点击确认')
          wx.openSetting({
            success: (res) => {}
          })
        } else {
          console.log('用户点击取消')
        }
      }
    });
  },
  editTabBar: function() {
    //使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组，数组最后一个就是当前页面。

    var curPageArr = getCurrentPages(); //获取加载的页面
    var curPage = curPageArr[curPageArr.length - 1]; //获取当前页面的对象
    var pagePath = curPage.route; //当前页面url
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }

    var tabBar = this.globalData.tabBar;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true; //根据页面地址设置当前页面状态    
      }
    }
    curPage.setData({
      tabBar: tabBar
    });


  },
  // 订单个数
  request: function() {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceOrder/sendFindAll',
      method: "post",
      data: {
        userId: wx.getStorageSync("userId")
      },
      success: function(res) {

        console.log(res)
        var title = "";
        var list = [];
        for (var i = 0; i < res.data.data.length; i++) {

          if (res.data.data[i].isCancel == true) {
            list = res.data.data.splice(i, 1);

          }
        }
        var length = [];

        for (var i = 0; i < res.data.data.length; i++) {
          if ((res.data.data[i].receiveUserIdArray.length > 0 && res.data.data[i].status == 4) || res.data.data[i].status == 10) {
            length.push(i);


          }

          // wx.setTabBarBadge({
          //   index: 1,
          //   text: "" + num + "",
          // })

        }
        var num = length.length;
        if (num == 0) {
          wx.removeTabBarBadge({
            index: 1,
          });
        } else {
          wx.setTabBarBadge({
            index: 1,
            text: "" + num + "",
          })
        }
        // that.globalData.num=num;
        // console.log(that.globalData.num)
        wx.setStorageSync("tabBarNum", num);
        console.log(num);
      }
    })
  },
  globalData: {
    isIphoneX: false,
    userInfo: null,
    num: 0,
    code: "",
    myUserInfo: false,
    phone: "",
    //第一种底部导航栏显示
    tabBar: {
      "color": "#aaa",
      "backgroundColor": "#fff",
      "borderStyle": "white",
      "selectedColor": "#0bbd79",
      "list": [{
          "pagePath": "../demand/demand",
          "text": "抢单",
          "iconPath": "../../icons/l1-07.png",
          "selectedIconPath": "../../icons/l1-07-pre.png",
          "clas": "menu-item",
          "selectedColor": "#4EDF80",
          "active": true
        },
        {
          "pagePath": "../orderList/orderList",
          "text": "订单",
          "iconPath": "../../icons/l1-04.png",
          "selectedColor": "#2d8cf0",
          "selectedIconPath": "../../icons/l1-04-pre.png",
          "clas": "menu-item",
          "selectedColor": "#4EDF80",
          "active": true
        },
        {
          "pagePath": "../mine/mine",
          "text": "我的",
          "selectedColor": "#2d8cf0",
          "iconPath": "../../icons/l1-06.png",
          "selectedIconPath": "../../icons/l1-06-pre.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item",
          "active": true
        }

      ],
      "position": "bottom"
    },
    //第二种底部导航栏显示
    tabBar1: {
      "color": "#9E9E9E",
      "selectedColor": "#f00",
      "backgroundColor": "#fff",
      "borderStyle": "#ccc",
      "list": [{
          "pagePath": "/pages/index/index",
          "text": "首页",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "clas": "menu-item1",
          "selectedColor": "#4EDF80",
          active: false
        },
        {
          "pagePath": "/pages/logs/logs",
          "text": "消息",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          active: true
        },
        {
          "pagePath": "/pages/cont/index",
          "text": "简历",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          active: false
        },
        {
          "pagePath": "/pages/detail/index",
          "text": "我的",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          active: false
        }
      ],
      "position": "bottom"
    }
  },


  /**
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide: function() {

  },

  /**
   * 当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
   */
  onError: function(msg) {

  }
})